﻿package mortal.game.view.common.menu
{
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import fl.controls.*;
    import fl.data.*;
    import fl.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.resource.*;

    public class ListMenu extends GUIComponent
    {
        private var _bg:ScaleBitmap;
        private var _list:List;
        private var _delayTimer:Timer;
        private var _dataProvider:DataProvider;
        private var _target:DisplayObject;

        public function ListMenu(param1:ScaleBitmap = null)
        {
            this._bg = this.addChild(param1 != null ? (param1) : (ResourceConst.getScaleBitmap(ImagesConst.ToolTipBg))) as ScaleBitmap;
            this._list = new GList();
            this._list.x = 0;
            this._list.y = 10;
            this._list.rowHeight = 18;
            this._list.horizontalScrollPolicy = "off";
            this._list.verticalScrollPolicy = "off";
            this._list.setStyle("skin", new Bitmap());
            this._list.setStyle("cellRenderer", ListMenuCellRenderer);
            this._list.addEventListener(ListEvent.ITEM_CLICK, this.itemClickHandler);
            this.addChild(this._list);
            setSize(86, 121);
            return;
        }// end function

        public function get dataProvider() : DataProvider
        {
            return this._dataProvider;
        }// end function

        public function set dataProvider(param1:DataProvider) : void
        {
            this._dataProvider = param1;
            this._list.dataProvider = param1;
            this._list.height = param1.length * this._list.rowHeight;
            this.height = this._list.height + 21;
            return;
        }// end function

        public function set target(param1:DisplayObject) : void
        {
            this._target = param1;
            return;
        }// end function

        public function get target() : DisplayObject
        {
            return this._target;
        }// end function

        public function hide() : void
        {
            this.visible = false;
            if (this.hasEventListener(Event.ENTER_FRAME))
            {
                this.removeEventListener(Event.ENTER_FRAME, this.enterFrameHandler);
            }
            Global.instance.callLater(this._list.clearSelection);
            return;
        }// end function

        public function show(param1:Number, param2:Number, param3:DisplayObject) : void
        {
            this.x = param1;
            this.y = param2;
            this.target = param3;
            if (!hasEventListener(MouseEvent.MOUSE_OVER))
            {
                this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler);
            }
            if (!this._delayTimer)
            {
                this._delayTimer = new Timer(3000);
                this._delayTimer.addEventListener(TimerEvent.TIMER, this.delayTimerHandler);
                this._delayTimer.start();
            }
            else
            {
                this._delayTimer.reset();
                this._delayTimer.start();
            }
            this.visible = true;
            return;
        }// end function

        private function enterFrameHandler(event:Event) : void
        {
            if (!this._bg.hitTestPoint(stage.mouseX, stage.mouseY, true))
            {
                this.hide();
            }
            return;
        }// end function

        private function mouseOverHandler(event:MouseEvent) : void
        {
            this._delayTimer.stop();
            this._delayTimer.removeEventListener(TimerEvent.TIMER, this.delayTimerHandler);
            this._delayTimer = null;
            this.removeEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler);
            this.addEventListener(Event.ENTER_FRAME, this.enterFrameHandler);
            return;
        }// end function

        private function delayTimerHandler(event:TimerEvent) : void
        {
            this._delayTimer.stop();
            this._delayTimer.removeEventListener(TimerEvent.TIMER, this.delayTimerHandler);
            this._delayTimer = null;
            this.hide();
            return;
        }// end function

        private function itemClickHandler(event:ListEvent) : void
        {
            switch(event.item)
            {
                case "":
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.hide();
            return;
        }// end function

        public function get list() : List
        {
            return this._list;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            this._bg.width = param1;
            this._list.width = param1;
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            this._bg.height = param1;
            return;
        }// end function

        override public function setStyle(param1:String, param2:Object) : void
        {
            this._list.setStyle(param1, param2);
            return;
        }// end function

    }
}
